<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('请假管理列表')"/>
    <style>
        .layui-card-body .layui-form {
            margin: auto;
        }
        .layui-form-item {
            margin-bottom: 0px;
        }
    </style>
</head>
<body class="layuimini-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form id="formId" class="layui-form layui-form-pane">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-inline">
                        <ul data-id="0" id="selectDept" name="deptId"></ul>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">用户</label>
                    <div class="layui-input-inline">
                        <input type="hidden" id="userId" name="userId">
                        <input type="text" id="username" name="username" placeholder="点击选择用户"
                               class="layui-input" />
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-md" lay-submit lay-filter="leave-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-md layui-btn-primary" id="formreset">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
        <table id="leave-table" lay-filter="leave-table"></table>
        <script type="text/html" id="leave-toolbar">
            <button sec:authorize="hasPermission('/kq/leave/add','kq:leave:add')"
                    class="layui-btn layui-btn-primary layui-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
            <button sec:authorize="hasPermission('/kq/leave/edit','kq:leave:edit')"
                    class="layui-btn layui-btn-primary layui-btn-md" lay-event="edit">
                <i class="layui-icon layui-icon-add-1"></i>
                发起流程
            </button>
            <button sec:authorize="hasPermission('/kq/leave/remove','kq:leave:del')"
                    class="layui-btn layui-btn-danger layui-btn-md" lay-event="batchRemove">
                <i class="layui-icon layui-icon-delete"></i>
                删除
            </button>
        </script>
    </div>
</div>
<th:block th:include="include :: footer"/>

<script id="state-templet" type="text/html">
    {{#if (d.state == '0' || d.state =='') { }}
    <span>起草中</span>
    {{# }else if(d.state == '1'){ }}
    <span>审批中</span>
    {{# }else if(d.state == 'yes'){ }}
    <span>审批通过</span>
    {{# } }}
</script>
<script>
    let table;
    let prefix = pName + "/kq/leave/";

    layui.use(['table', 'form', 'jquery', 'dtree'], function () {
        table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;


        deptUtil.initDeptSelectTree("selectDept", "deptId", "deptName", 1)

        $("#username").on("click", function () {
            userUtil.initUserSelect("userId", "username");
        });

        let cols = [
            [
                {type: 'checkbox'},
                {
                    field: 'id',
                    title: '请假信息主键',
                    hide: true
                },
                {
                    field: 'userSn',
                    title: '请假人工号'
                },
                {
                    field: 'username',
                    title: '请假人名称'
                },

                {
                    field: 'startDateStr',
                    title: '开始日期'
                },

                {
                    field: 'endDateStr',
                    title: '结束日期'
                },

                {
                    field: 'leaveTypeName',
                    title: '假别'
                },
                {
                    field: 'days',
                    title: '请假天数'
                },
                {
                    field: 'state',
                    title: '状态',
                    templet: "#state-templet"
                }
            ]
        ]

        table.render({
            elem: '#leave-table',
            url: prefix + 'data',
            page: true,
            escape: true,
            cols: cols,
            skin: 'line',
            toolbar: '#leave-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
            ,height: 'full-100'
        });
        table.on('checkbox(leave-table)', function(obj){

        });
        table.on('row(leave-table)', function (obj) {

        });
        table.on('toolbar(leave-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'edit') {
                window.edit();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(leave-query)', function (data) {
            table.reload('leave-table', {where: data.field})
            return false;
        });


        window.refresh = function (param) {
            table.reload('leave-table', {where: param});
        };
        $('#formreset').click(function(){
            $('#formId').find('input').val("")
        })
    })
</script>
<script sec:authorize="hasPermission('/kq/leave/add','kq:leave:add')">
    window.add = function () {
        let layerIndex = layer.open({
            type: 2,
            title: '新增请假管理',
            shade: 0.1,
            maxmin : true,
            area: ['99%', '99%'],
            content: prefix + 'add'
        });
        $(window).on("resize", function () {
            layer.full(layerIndex);
        });
        return false;
    }
</script>
<script sec:authorize="hasPermission('/kq/leave/edit','kq:leave:edit')">

    window.edit = function (obj) {

        let loading = layer.load();
        $.ajax({
            url: pName + '/process/start',
            data: JSON.stringify({"key":"process_leave"}),
            dataType: 'json',
            contentType: 'application/json',
            type: 'post',
            success: function (result) {
                layer.close(loading);
                if (result.code == 1) {
                    let layerIndex = top.layer.open({
                        id: "leave_start",
                        type: 2,
                        title: '发起请假',
                        shade: 0.1,
                        maxmin : true,
                        area: ['99%', '99%'],
                        content: pName + result.data,
                        cancel: function (index, layero) {
                            let iframeWindow = top.layer.getChildFrame('body', index); // 通过 index 参数获取 iframe 的内容
                            let iframe1 = iframeWindow.find("#iframe1");
                            top.layer.confirm('是否删除流程？', {
                                btn: ['是', '否'] //可以无限个按钮
                            }, function (index) {
                                top.layer.close(index);
                                let btn = iframe1[0].contentWindow.$("#shutdown");
                                btn.click();
                            }, function (index) {
                                top.layer.close(index);
                                let btn = iframe1[0].contentWindow.$("#saveBtn");
                                btn.click();
                                top.layer.close(layerIndex);

                                table.reload('leave-table')

                            });
                            return false; // 阻止默认的关闭操作
                        }

                    });
                    console.log(layui.layer.getChildFrame('body', layerIndex));
                    $(window).on("resize", function () {
                        layer.full(layerIndex);
                    });
                    return false;
                } else {
                    layer.alert(result.msg, {icon: 2});
                }
            }
        })
        return false;
    }

    window.remove = function (obj) {
        layer.confirm('确定要删除该请假管理', {icon: 3, title: '提示'}, function (index) {
            layer.close(index);
            let loading = layer.load();
            $.ajax({
                url: prefix + "remove/" + obj.data['id'],
                dataType: 'json',
                type: 'delete',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            obj.del();
                        });
                    } else {
                        layer.alert(result.msg, {icon: 2});
                    }
                }
            })
        });
    }

    window.batchRemove = function (obj) {
        let data = table.checkStatus(obj.config.id).data;
        if (data.length === 0) {
            layer.msg("未选中数据", {icon: 3, time: 1000});
            return false;
        }
        let ids = "";
        for (let i = 0; i < data.length; i++) {
            ids += data[i].id + ",";
        }
        ids = ids.substr(0, ids.length - 1);
        layer.confirm('确定要删除这些请假管理', {icon: 3, title: '提示'}, function (index) {
            layer.close(index);
            let loading = layer.load();
            $.ajax({
                url: prefix + "batchRemove",
                dataType: 'json',
                data: {"ids": ids},
                type: 'delete',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            table.reload('leave-table');
                        });
                    } else {
                        layer.alert(result.msg, {icon: 2});
                    }
                }
            })
        });
    }
</script>
<script sec:authorize="hasPermission('/kq/leave/del','kq:leave:del')">

    window.remove = function (obj) {
        layer.confirm('确定要删除该请假管理', {icon: 3, title: '提示'}, function (index) {
            layer.close(index);
            let loading = layer.load();
            $.ajax({
                url: prefix + "remove/" + obj.data['id'],
                dataType: 'json',
                type: 'delete',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            obj.del();
                        });
                    } else {
                        layer.alert(result.msg, {icon: 2});
                    }
                }
            })
        });
    }

    window.batchRemove = function (obj) {
        let data = table.checkStatus(obj.config.id).data;
        if (data.length === 0) {
            layer.msg("未选中数据", {icon: 3, time: 1000});
            return false;
        }
        let ids = "";
        for (let i = 0; i < data.length; i++) {
            ids += data[i].id + ",";
        }
        ids = ids.substr(0, ids.length - 1);
        layer.confirm('确定要删除这些请假管理', {icon: 3, title: '提示'}, function (index) {
            layer.close(index);
            let loading = layer.load();
            $.ajax({
                url: prefix + "batchRemove",
                dataType: 'json',
                data: {"ids": ids},
                type: 'delete',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            table.reload('leave-table');
                        });
                    } else {
                        layer.alert(result.msg, {icon: 2});
                    }
                }
            })
        });
    }
</script>
</body>
</html>